package com.hs.homework_01.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hs.homework_01.entity.Student;
import com.hs.homework_01.mapper.StudentMapper;
import com.hs.homework_01.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName StudentServiceImpl
 * @Description TODO
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper mapper;

    @Override
    public PageInfo<Student> findByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Student> students = mapper.selectAll();
        return new PageInfo<>(students);
    }

    @Override
    public Student findById(Integer id) {
        return mapper.selectById(id);
    }

    @Override
    public int updateStudent(Student student) {
        int update = mapper.update(student);
        if (update < 1) {
            throw  new RuntimeException("更新失败");
        }
        return update;
    }

    @Override
    public int addStudent(Student student) {
        int insert = mapper.insert(student);
        if(insert < 1) {
            throw new RuntimeException("新增失败");
        }
        return insert;
    }

    @Override
    public int removeStudent(Integer id) {
        int i = mapper.deleteById(id);
        if(i < 1) {
            throw new RuntimeException("删除失败");
        }
        return 0;
    }
}
